home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS16.ADF / C / InputHandler / in1.c < prev    next >
C/C++ Source or Header  |  1989-01-27  |  9KB  |  349 lines

  1. #include <exec/types.h>
  2. #include <exec/ports.h>
  3. #define function     /*  this is just used by the indenter  */
  4. #define then     {
  5. #define elsif    } else if
  6. #define els      } else  {
  7. #define fi       }
  8. #define forever  for (;;) {
  9. #define rof      }
  10. #define or ||
  11. #define and &&
  12. #define not !
  13. #define z if(1==0)
  14. #include <devices/timer.h>
  15. #include <exec/memory.h>
  16. /* #include <exec/io.h>    */
  17. /* #include <exec/tasks.h> */
  18. #include <exec/interrupts.h>
  19. #include <devices/input.h>
  20. #include <exec/devices.h>
  21. #include <devices/inputevent.h>
  22. #include <stdio.h>
  23.  
  24. #define F1KEYUP 0xD0
  25.  
  26. struct InputEvent  copyevent;
  27. struct MsgPort    *inputDevPort;
  28. struct IOStdReq   *inputRequestBlock;
  29. struct Interrupt   handlerStuff;
  30. struct InputEvent  dummyEvent;
  31. struct MsgPort    *CreatePort();
  32. struct IOStdReq   *CreateStdIO();
  33. struct MemEntry    me[10];
  34. struct timerequest *PrepareTimer();
  35.  
  36. extern long OpenDevice();
  37. extern Enable_Abort;
  38. extern long SysBase;
  39.  
  40. int hcount = 0;
  41. long int p1,p2,p3;
  42. static     char     str_buf[BUFSIZ];  /* used for buffered i/o  to terminal */
  43.  
  44. function struct InputEvent *myhandler(ev,mydata) struct InputEvent *ev; struct MemEntry *mydata[]; {
  45.  
  46.     p3 = SysBase;
  47.     hcount++; p1 = (long)ev; p2 = (long)mydata;
  48.     /* return ev; */
  49.     if(ev->ie_Class == IECLASS_TIMER) then
  50.         return(ev);
  51.     els    
  52.         Forbid();
  53.         z Disable();
  54.         copyevent.ie_NextEvent        = ev->ie_NextEvent;
  55.         copyevent.ie_Class        = ev->ie_Class;
  56.         copyevent.ie_SubClass        = ev->ie_SubClass;
  57.         copyevent.ie_Code        = ev->ie_Code;
  58.         copyevent.ie_Qualifier        = ev->ie_Qualifier;
  59.         copyevent.ie_X            = ev->ie_X;
  60.         copyevent.ie_Y            = ev->ie_Y;
  61.         copyevent.ie_TimeStamp.tv_secs    = ev->ie_TimeStamp.tv_secs;
  62.         copyevent.ie_TimeStamp.tv_micro    = ev->ie_TimeStamp.tv_micro;
  63.         z Enable();
  64.         Permit();
  65.     fi
  66.     return ev;
  67.     
  68. }    
  69.  
  70. extern struct Task *FindTask();
  71. struct Task *mytask;
  72. LONG mysingnal;
  73. extern VOID HandlerInterface();
  74.  
  75. struct timerequest *mytimerRequest;
  76. extern struct timerequest *PrepareTimer();
  77. extern long int WaitTimer();
  78. extern void     DeleteTimer();
  79.  
  80. LONG my_a4 = 0; extern LONG GetA4();
  81. function void main(argc,argv) int argc; char **argv; {
  82.  
  83.     long error;
  84.     ULONG oldseconds,oldmicro,oldclass;
  85.     
  86.     my_a4 = GetA4();  /* need current base for interrupt handler */
  87.     
  88.  
  89.     dummyEvent.ie_Class = IECLASS_NULL;
  90.     dummyEvent.ie_NextEvent = NULL;
  91.  
  92.     inputDevPort = CreatePort(0L,0L);
  93.     if(inputDevPort == NULL) then
  94.         printf("cant open dev port\n");
  95.         exit(-1);
  96.     fi
  97.  
  98.     inputRequestBlock = CreateStdIO(inputDevPort);
  99.     if (inputRequestBlock == NULL) then 
  100.         DeletePort(inputDevPort);
  101.         printf("cant create stdio\n");
  102.         exit(-2);
  103.     fi
  104.     mytimerRequest = PrepareTimer(1);
  105.     if (mytimerRequest == NULL) then
  106.         printf("cant open timer\n");
  107.         exit(-3);
  108.     fi
  109.  
  110.     handlerStuff.is_Data       = (APTR) &me[0];
  111.     handlerStuff.is_Code       = HandlerInterface;
  112.     handlerStuff.is_Node.ln_Pri= 51;
  113.     printf("addr of handler   = %08lx\n",HandlerInterface);
  114.     printf("addr of myhandler = %08lx\n",myhandler);
  115.     printf("addr of mydata    = %08lx\n\n\n\n\n",&me[0]   );
  116.  
  117.     error = OpenDevice("input.device",0L,inputRequestBlock,0L);
  118.     if (error == NULL ) then
  119.         printf("Opened the input device\n");
  120.     fi
  121.     inputRequestBlock->io_Command = IND_ADDHANDLER;
  122.     inputRequestBlock->io_Data    = (APTR) &handlerStuff;
  123.  
  124.     DoIO(inputRequestBlock); 
  125.     copyevent.ie_TimeStamp.tv_secs  = 0;
  126.     copyevent.ie_TimeStamp.tv_micro = 0;
  127.     copyevent.ie_Class         = 0;
  128.  
  129.  
  130.     oldseconds = 0;
  131.     oldmicro   = 0;
  132.     oldclass   = 0;
  133.  
  134.     Enable_Abort = 0;
  135.     forever
  136.         if ( Chk_Abort() ) then
  137.             printf("aborting\n");
  138.             break;
  139.         fi
  140.         WaitForTimer(mytimerRequest,0L,100000L);
  141.         if(copyevent.ie_Class == IECLASS_RAWKEY && copyevent.ie_Code == F1KEYUP) then
  142.             break;
  143.         els    
  144.               z printf("checking %4d %08lx %08lx\n",hcount,p1,p2);
  145.             Forbid();
  146.             if (copyevent.ie_TimeStamp.tv_secs != oldseconds or
  147.                    copyevent.ie_TimeStamp.tv_micro != oldmicro or
  148.                                 copyevent.ie_Class != oldclass )  then
  149.         
  150.                 oldseconds = copyevent.ie_TimeStamp.tv_secs;  
  151.                 oldmicro   = copyevent.ie_TimeStamp.tv_micro; 
  152.                 oldclass   = copyevent.ie_Class;         
  153.                 showEvents(©event);
  154.             fi    
  155.             Permit();
  156.       
  157.         fi
  158.     rof
  159.     inputRequestBlock->io_Command = IND_REMHANDLER;
  160.     inputRequestBlock->io_Data    = (APTR) &handlerStuff;
  161.     DoIO(inputRequestBlock); 
  162.  
  163.  
  164.     CloseDevice(inputRequestBlock);
  165.     DeleteStdIO(inputRequestBlock);
  166.     DeletePort(inputDevPort);
  167.     DeleteTimer(mytimerRequest);
  168. }
  169.  
  170. function showEvents(e) struct InputEvent *e; {
  171.     static FILE *evio = NULL;
  172.     short int cd;
  173.     
  174.     if (evio == NULL) then
  175.         evio = fopen("con:0/0/630/180/evio","w");
  176.         if (evio == NULL) then
  177.            printf("cant open evio\n");
  178.            return;
  179.         fi
  180.         setbuf(evio,str_buf);
  181.     fi
  182.     
  183.     fprintf(evio,"--------------------------------------------------\n");
  184.     if    (e->ie_Class ==  IECLASS_RAWKEY or e->ie_Class ==  IECLASS_RAWMOUSE) then
  185.             if    (e->ie_Class ==  IECLASS_RAWKEY ) then
  186.             fprintf(evio,"Class     = Key  : ");
  187.             elsif (e->ie_Class ==  IECLASS_RAWMOUSE) then
  188.             fprintf(evio,"Class     = Mouse: ");
  189.             fi    
  190.             
  191.         if (e->ie_Qualifier & IEQUALIFIER_RSHIFT) then
  192.             fprintf(evio," rshift ");
  193.         fi    
  194.         if (e->ie_Qualifier & IEQUALIFIER_LSHIFT) then
  195.             fprintf(evio," lshift ");
  196.         fi    
  197.         if (e->ie_Qualifier & IEQUALIFIER_CAPSLOCK) then
  198.             fprintf(evio," capslock ");
  199.         fi    
  200.         if (e->ie_Qualifier & IEQUALIFIER_CONTROL) then
  201.             fprintf(evio," control ");
  202.         fi    
  203.         if (e->ie_Qualifier & IEQUALIFIER_LCOMMAND) then
  204.             fprintf(evio," lcmd ");
  205.         fi    
  206.         if (e->ie_Qualifier & IEQUALIFIER_RCOMMAND) then
  207.             fprintf(evio," rcmd ");
  208.         fi    
  209.         if (e->ie_Qualifier & IEQUALIFIER_LALT) then
  210.             fprintf(evio," lalt ");
  211.         fi    
  212.         if (e->ie_Qualifier & IEQUALIFIER_RALT) then
  213.             fprintf(evio," ralt ");
  214.         fi    
  215.         if (e->ie_Qualifier & IEQUALIFIER_LBUTTON) then
  216.             fprintf(evio," lbut ");
  217.         fi    
  218.         if (e->ie_Qualifier & IEQUALIFIER_RBUTTON) then
  219.             fprintf(evio," rbut ");
  220.         fi    
  221.         
  222.         fprintf(evio,"\n");
  223.     els    
  224.         fprintf(evio,"Class     = %x\n",e->ie_Class);
  225.     fi
  226.     fprintf(evio,"SubClass  = %x\n",e->ie_SubClass);
  227.         if    (e->ie_Class ==  IECLASS_RAWMOUSE ) then
  228.               cd = e->ie_Code & 0x7f;
  229.           fprintf(evio,"Code      = %x ",e->ie_Code);
  230.           if (cd  == IECODE_LBUTTON) then
  231.             fprintf(evio," lbut ");
  232.           fi
  233.           if (cd  == IECODE_RBUTTON) then
  234.             fprintf(evio," rbut ");
  235.           fi
  236.           if (e->ie_Code & IECODE_UP_PREFIX and e->ie_Code != IECODE_NOBUTTON ) then
  237.             fprintf(evio," UP ");
  238.           fi
  239.           fprintf(evio,"\n");
  240.     els    
  241.         fprintf(evio,"CODE      = %x\n",e->ie_Code);
  242.     fi
  243.     fprintf(evio,"Qualifier = %x\n",e->ie_Qualifier);
  244.     fprintf(evio,"X         = %d\n",e->ie_X);
  245.     fprintf(evio,"Y         = %d\n",e->ie_Y);
  246.     fprintf(evio,"TimeStamp = %lx\n",e->ie_TimeStamp.tv_secs);
  247.     fprintf(evio,"next      = %08lx p3 = %08lx\n",e->ie_NextEvent,p3);
  248.     fflush(evio);
  249. }
  250.  
  251.  
  252. extern struct MsgPort *CreatePort();
  253. extern struct IORequest *CreateExtIO();
  254.  
  255.  
  256. function struct timerequest *PrepareTimer(precision) SHORT precision; {
  257.  
  258.     long int error; long whichunit;
  259.  
  260.     struct MsgPort *timerport;
  261.     struct timerequest *timermsg;
  262.  
  263.     timerport = CreatePort(0L,0L);
  264.  
  265.     if (timerport == NULL) then
  266.         printf("no port\n");
  267.         return NULL;
  268.     fi
  269.  
  270.  
  271.     timermsg =(struct timerequest *) CreateExtIO(timerport,(long)sizeof(struct timerequest));
  272.     if (timermsg == NULL) then
  273.         printf("no timermsg\n");
  274.         DeletePort(timerport);
  275.         return NULL;
  276.  
  277.     fi
  278.  
  279.     if (precision) then
  280.         whichunit = UNIT_MICROHZ;
  281.     els
  282.         whichunit = UNIT_VBLANK;
  283.     fi
  284.  
  285.     error = OpenDevice(TIMERNAME,whichunit,timermsg,0L);
  286.     if (error != NULL) then
  287.         printf("opendevice = %ld\n",error);
  288.         DeleteExtIO(timermsg,sizeof(struct timerequest));
  289.         DeletePort(timerport);
  290.         return NULL;
  291.     fi
  292.     return timermsg;
  293.  
  294. }
  295.  
  296. function WaitForTimer(tr,seconds,microseconds) 
  297.             ULONG seconds,microseconds; struct timerequest *tr; {
  298.  
  299.     tr->tr_node.io_Command = TR_ADDREQUEST;
  300.     tr->tr_time.tv_secs    = seconds;
  301.     tr->tr_time.tv_micro   = microseconds;
  302.     DoIO(tr);
  303.  
  304.     return 0;
  305.  
  306.  
  307. }
  308.  
  309.  
  310. function void DeleteTimer(tr) struct timerequest *tr; {
  311.     struct MsgPort *tp;
  312.     
  313.     tp = tr->tr_node.io_Message.mn_ReplyPort;
  314.     if (tr != 0) then
  315.         CloseDevice(tr);
  316.         DeleteExtIO(tr,sizeof(struct timerequest));
  317.     fi    
  318.     if (tp != 0) then
  319.         DeletePort(tp);
  320.     fi
  321.     return ;
  322. #ifndef _lint    
  323. #asm
  324.  
  325.  
  326. ;-------------------------------------------------------------------------
  327. ; MAKE SURE THIS ASSEMBLY CODE IS AT THE END OF THIS C SOURCE FILE
  328. ;-------------------------------------------------------------------------
  329.     far code    ;make sure the below can load a4 without using a4
  330.     far data    ;ditto
  331. _HandlerInterface:
  332.     movem.l        saver,-(sp)
  333.     move.l        _my_a4,a4
  334.     movem.l        a0/a1,-(a7)
  335.     
  336.     jsr        _myhandler
  337.     addq.l        #8,a7
  338.     movem.l        (sp)+,saver
  339.     rts
  340. saver:    reg    a0-a6/d1-d7
  341.  
  342. _GetA4:
  343.     move.l        a4,d0
  344.     rts
  345.  
  346. #endasm
  347. #endif
  348. }
  349.